using System;
using System.ComponentModel;
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ICP_MS.Plugin.Tune;

public class FormNewLens : Form
{
	private bool isDispose = false;

	private ToolTip ttpSettings = new ToolTip();

	private IContainer components = null;

	private FlowLayoutPanel flowLayoutPanel1;

	private TurnCntlFeed tcfFocus;

	private TurnCntlFeed tcfHexBias;

	private TurnCntlFeed tcfQE;

	public FormNewLens()
	{
		InitializeComponent();
		ttpSettings.InitialDelay = 200;
		ttpSettings.AutoPopDelay = 10000;
		ttpSettings.ReshowDelay = 200;
		ttpSettings.ShowAlways = true;
		ttpSettings.IsBalloon = true;
	}

	private void InitConfigs(Platform plat)
	{
		try
		{
			tcfHexBias.Value = Convert.ToDecimal(LensManager.newLens.HexBias);
			tcfFocus.Value = Convert.ToDecimal(LensManager.newLens.Focus);
			tcfQE.Value = Convert.ToDecimal(LensManager.newLens.QE);
		}
		catch
		{
			Log.InsertLog("Minor", "Params exceed limit.", "", (Severity)3);
		}
	}

	private void TermConfigs()
	{
		RemoveConfigsMsg();
	}

	private void AddConfigsMsg()
	{
		tcfQE.ValueChanged += Minor_ValueChanged;
		tcfFocus.ValueChanged += Minor_ValueChanged;
		tcfHexBias.ValueChanged += Minor_ValueChanged;
	}

	private void RemoveConfigsMsg()
	{
		tcfQE.ValueChanged -= Minor_ValueChanged;
		tcfFocus.ValueChanged -= Minor_ValueChanged;
		tcfHexBias.ValueChanged -= Minor_ValueChanged;
	}

	private void Minor_ValueChanged(object sender, EventArgs e)
	{
		string name = (sender as TurnCntlFeed).Name;
		double value = decimal.ToDouble((sender as TurnCntlFeed).Value);
		double quadVacuum = Vacuum.vacumm.ReadVacuum[2];
		int status1 = Vacuum.vacumm.ReadVacuumStatus[2];
		bool bright = Vacuum.Instance.QuadVacuumOK || UserManager.CurrentUser.authority > 1;
		if (name == tcfFocus.Name)
		{
			if (bright || UserManager.CurrentUser.authority > 1)
			{
				LensManager.newLens.Focus = (float)value;
				LensManager.Instance.SetNewFocus();
			}
			else
			{
				MessageBox.Show("四极杆真空未达到！不能设置聚焦透镜电压", Lang.Txt("AddFormIgnite.Warn"));
			}
		}
		else if (name == tcfHexBias.Name)
		{
			if (bright || UserManager.CurrentUser.authority > 1)
			{
				LensManager.newLens.HexBias = (float)value;
				LensManager.Instance.SetHexBias();
			}
			else
			{
				MessageBox.Show("四极杆真空未达到！不能设置六极杆偏置电压", Lang.Txt("AddFormIgnite.Warn"));
			}
		}
		else if (name == tcfQE.Name)
		{
			if (bright || UserManager.CurrentUser.authority > 1)
			{
				LensManager.newLens.QE = (float)value;
				LensManager.Instance.SetQE();
			}
			else
			{
				MessageBox.Show("四极杆真空未达到,不能设置QE电压！", Lang.Txt("AddFormIgnite.Warn"));
			}
		}
	}

	private void FormLens_Load(object sender, EventArgs e)
	{
		tcfHexBias.TipMax = 10.0;
		tcfHexBias.TipMin = -10.0;
		tcfFocus.TipMin = -50.0;
		tcfFocus.TipMax = -50.0;
		InitConfigs(PlatManager.plat);
		AddConfigsMsg();
		Task.Factory.StartNew(delegate
		{
			while (!isDispose)
			{
				try
				{
					Invoke((MethodInvoker)delegate
					{
					});
				}
				catch
				{
				}
				Thread.Sleep(1000);
			}
		});
		ApplyResources();
	}

	private void Tcf_MouseEnter(object sender, EventArgs e)
	{
		Control ctrl = (Control)sender;
		string tipOverwrite = " ";
		switch (ctrl.Name)
		{
		case "tcfQE":
			tipOverwrite = "建议四级前置：-30~-10";
			break;
		case "tcfLens3":
			tipOverwrite = "建议透镜3：-200~-150";
			break;
		case "tcfLens2":
			tipOverwrite = "建议透镜2：-90~-70";
			break;
		case "tcfDA":
			tipOverwrite = "建议偏转辅助：-45~-30";
			break;
		case "tcfD2":
			tipOverwrite = "建议偏转电压：-45~-25";
			break;
		}
		ttpSettings.SetToolTip(ctrl, tipOverwrite);
	}

	private void ApplyResources()
	{
	}

	private void FormLens_FormClosing(object sender, FormClosingEventArgs e)
	{
		isDispose = true;
		TermConfigs();
	}

	public void ConfigsChanged(object sender, EventArgs e)
	{
		RemoveConfigsMsg();
		InitConfigs((Platform)sender);
		AddConfigsMsg();
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
		this.tcfFocus = new ICP_MS.Plugin.Tune.TurnCntlFeed();
		this.tcfHexBias = new ICP_MS.Plugin.Tune.TurnCntlFeed();
		this.tcfQE = new ICP_MS.Plugin.Tune.TurnCntlFeed();
		this.flowLayoutPanel1.SuspendLayout();
		base.SuspendLayout();
		this.flowLayoutPanel1.AutoScroll = true;
		this.flowLayoutPanel1.Controls.Add(this.tcfFocus);
		this.flowLayoutPanel1.Controls.Add(this.tcfHexBias);
		this.flowLayoutPanel1.Controls.Add(this.tcfQE);
		this.flowLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
		this.flowLayoutPanel1.Location = new System.Drawing.Point(0, 0);
		this.flowLayoutPanel1.Margin = new System.Windows.Forms.Padding(4);
		this.flowLayoutPanel1.Name = "flowLayoutPanel1";
		this.flowLayoutPanel1.Size = new System.Drawing.Size(293, 975);
		this.flowLayoutPanel1.TabIndex = 43;
		this.tcfFocus.C = 0.0;
		this.tcfFocus.CodeId = 0u;
		this.tcfFocus.Coe = new decimal(new int[4] { 1, 0, 0, 0 });
		this.tcfFocus.DecimalPlaces = 2;
		this.tcfFocus.Defalut = 0.0;
		this.tcfFocus.DestId = 0u;
		this.tcfFocus.FeedDecimal = 2;
		this.tcfFocus.Increment = new decimal(new int[4] { 1, 0, 0, 0 });
		this.tcfFocus.K = 0.0;
		this.tcfFocus.Label = "Focus";
		this.tcfFocus.Location = new System.Drawing.Point(7, 6);
		this.tcfFocus.Margin = new System.Windows.Forms.Padding(7, 6, 7, 6);
		this.tcfFocus.MaxValue = new decimal(new int[4] { 50, 0, 0, 0 });
		this.tcfFocus.MinValue = new decimal(new int[4] { 50, 0, 0, -2147483648 });
		this.tcfFocus.Name = "tcfFocus";
		this.tcfFocus.Size = new System.Drawing.Size(256, 68);
		this.tcfFocus.TabIndex = 45;
		this.tcfFocus.TipMax = double.NaN;
		this.tcfFocus.TipMin = double.NaN;
		this.tcfFocus.Value = new decimal(new int[4]);
		this.tcfHexBias.C = 0.0;
		this.tcfHexBias.CodeId = 0u;
		this.tcfHexBias.Coe = new decimal(new int[4] { 1, 0, 0, 0 });
		this.tcfHexBias.DecimalPlaces = 2;
		this.tcfHexBias.Defalut = 0.0;
		this.tcfHexBias.DestId = 0u;
		this.tcfHexBias.FeedDecimal = 2;
		this.tcfHexBias.Increment = new decimal(new int[4] { 1, 0, 0, 0 });
		this.tcfHexBias.K = 0.0;
		this.tcfHexBias.Label = "HexBias";
		this.tcfHexBias.Location = new System.Drawing.Point(7, 86);
		this.tcfHexBias.Margin = new System.Windows.Forms.Padding(7, 6, 7, 6);
		this.tcfHexBias.MaxValue = new decimal(new int[4] { 10, 0, 0, 0 });
		this.tcfHexBias.MinValue = new decimal(new int[4] { 10, 0, 0, -2147483648 });
		this.tcfHexBias.Name = "tcfHexBias";
		this.tcfHexBias.Size = new System.Drawing.Size(256, 68);
		this.tcfHexBias.TabIndex = 46;
		this.tcfHexBias.TipMax = double.NaN;
		this.tcfHexBias.TipMin = double.NaN;
		this.tcfHexBias.Value = new decimal(new int[4]);
		this.tcfQE.C = 0.0;
		this.tcfQE.CodeId = 0u;
		this.tcfQE.Coe = new decimal(new int[4] { 1, 0, 0, 0 });
		this.tcfQE.DecimalPlaces = 2;
		this.tcfQE.Defalut = 0.0;
		this.tcfQE.DestId = 0u;
		this.tcfQE.FeedDecimal = 2;
		this.tcfQE.Increment = new decimal(new int[4] { 1, 0, 0, 0 });
		this.tcfQE.K = 0.0;
		this.tcfQE.Label = "QE";
		this.tcfQE.Location = new System.Drawing.Point(7, 166);
		this.tcfQE.Margin = new System.Windows.Forms.Padding(7, 6, 7, 6);
		this.tcfQE.MaxValue = new decimal(new int[4] { 50, 0, 0, 0 });
		this.tcfQE.MinValue = new decimal(new int[4] { 50, 0, 0, -2147483648 });
		this.tcfQE.Name = "tcfQE";
		this.tcfQE.Size = new System.Drawing.Size(256, 68);
		this.tcfQE.TabIndex = 45;
		this.tcfQE.TipMax = double.NaN;
		this.tcfQE.TipMin = double.NaN;
		this.tcfQE.Value = new decimal(new int[4]);
		base.AutoScaleDimensions = new System.Drawing.SizeF(8f, 15f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		this.AutoSize = true;
		base.ClientSize = new System.Drawing.Size(293, 975);
		base.Controls.Add(this.flowLayoutPanel1);
		base.Margin = new System.Windows.Forms.Padding(5);
		base.Name = "FormNewLens";
		base.ShowIcon = false;
		this.Text = "FormX2Lens";
		base.FormClosing += new System.Windows.Forms.FormClosingEventHandler(FormLens_FormClosing);
		base.Load += new System.EventHandler(FormLens_Load);
		this.flowLayoutPanel1.ResumeLayout(false);
		base.ResumeLayout(false);
	}
}
